using UnityEngine;
using System.Collections;

public class MouseScript : MonoBehaviour {
	
	public Vector3 DesiredVelocity;
	public Vector3 DesiredPosition;
	private int NumDeath = 0;
	private int NumFood = 0;
	
	// Use this for initialization
	void Start () {
	}
	
	void FixedUpdate () {
		if (DesiredVelocity != Vector3.zero)
		{
			Vector3 goPos = rigidbody.position + DesiredVelocity * Time.fixedDeltaTime;
			rigidbody.MovePosition(goPos);
		}
		
	    if (DesiredVelocity.sqrMagnitude >= 0.1f)
		{
			Quaternion r = Quaternion.LookRotation(DesiredVelocity, Vector3.up);
			transform.rotation = Quaternion.RotateTowards(transform.rotation, r, 10f);
		}
		
	}
	
	// Update is called once per frame
	void Update () {
		//rigidbody.rotation = Quaternion.identity;
	}
	
	void OnCollisionEnter(Collision collisionInfo)
	{
		BallScript ball = collisionInfo.gameObject.GetComponent<BallScript>();
		if (ball)
		{
			if (ball.IsBad)
			{
				// restart game
				Vector3 randomPos = Random.insideUnitSphere * 4.0f;
				ball.rigidbody.position = new Vector3(randomPos.x, 1, randomPos.z);
	
				float angle = Random.Range(0, 2 * Mathf.PI);
				ball.rigidbody.velocity =
					new Vector3(TestScript.BallMaxSpeed * Mathf.Cos(angle), 0,
								TestScript.BallMaxSpeed * Mathf.Sin(angle));
				
				randomPos = Random.insideUnitSphere * 4.0f;
				rigidbody.position = new Vector3(randomPos.x, 1, randomPos.z);
				
				NumDeath++;
			}
			else
			{
				Vector3 randomPos = Random.insideUnitSphere * 4.0f;
				ball.rigidbody.position = new Vector3(randomPos.x, 1, randomPos.z);
	
				float angle = Random.Range(0, 2 * Mathf.PI);
				ball.rigidbody.velocity =
					new Vector3(TestScript.BallMaxSpeed * Mathf.Cos(angle), 0,
								TestScript.BallMaxSpeed * Mathf.Sin(angle));
				
				NumFood++;
			}
			
			print("Deaths: " + NumDeath + " Food: " + NumFood);
		}
		
	}
}
